package com.hdoit.apollo.web.shop.controllers;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hdoit.apollo.logical.shop.model.Shop;
import com.hdoit.apollo.logical.shop.model.Staff;
import com.hdoit.apollo.logical.shop.service.IStaffService;
import com.hdoit.apollo.logical.viewmodel.shop.StaffSearch;
import com.tennetcn.authority.common.enums.SexStatus;
import com.tennetcn.authority.common.message.AuthorityLoginUser;
import com.tennetcn.authority.component.web.ActionResult;
import com.tennetcn.authority.component.web.BaseController;
import com.tennetcn.authority.component.web.ErrorResult;
import com.tennetcn.authority.component.web.JsonActionResult;
import com.tennetcn.authority.logical.service.ILoginUserService;
import com.tennetcn.core.common.enums.pub.YesOrNoInteger;
import com.tennetcn.core.common.message.ManageContainer;
import com.tennetcn.core.common.utils.ChineseUtils;
import com.tennetcn.core.common.utils.CommonUtils;
import com.tennetcn.core.common.utils.DateUtils;
import com.tennetcn.data.enums.ModelStatus;
import com.tennetcn.data.message.PagerModel;
import com.tennetcn.web.message.WebManageContainer;

/**
 * @author      tancongzhu
 * @email       
 * @createtime  2018年04月26日 14:46:17
 * @comment
 */
@Controller
@RequestMapping(value = "/shop/staff/")
public class StaffController extends BaseController{
    @Override
    public String getRoutePath() {
        return "areas/shop/staff/";
    }
    
    @Autowired
    private IStaffService staffService;
    
    @Autowired
    private ILoginUserService loginUserService;

    private final String indexViewName="index";
    private final String outputViewName="output";
    private final String detailedViewName="detailed";
    private final String saveViewName="save";
    private final String deleteViewName = "delete";
    private final String checkMobileViewName="checkMobile";

    @RequestMapping(value = indexViewName, method = { RequestMethod.GET,RequestMethod.POST })
    public String index(Model model,PagerModel pagerModel,StaffSearch search,boolean ajaxPost){

        pagerModel.setTotalAmount(staffService.queryCountMP(search));
        List<Staff> staffs= staffService.queryListMP(search,pagerModel);

        model.addAttribute(WebManageContainer.PAGERKEY, pagerModel);
        model.addAttribute("staffs", staffs);
        model.addAttribute("staffSearch", search);

        if(ajaxPost){
            return view(outputViewName);
        }

        return view(indexViewName);
    }

    @RequestMapping(value = detailedViewName, method = { RequestMethod.GET})
    public String staffDetailed(Model model,String id){
        Staff staff=new Staff();
        if(!StringUtils.isEmpty(id)){
            staff=staffService.queryModel(id);
        }
        model.addAttribute("staff", staff);
        return view(detailedViewName);
    }

    @RequestMapping(value = saveViewName, method = { RequestMethod.POST})
    public String save(Model model,Staff staff,String newPassword){
    	AuthorityLoginUser authorityLoginUser=new AuthorityLoginUser();
    	
        if(StringUtils.isEmpty(staff.getId())){
            staff.setId(CommonUtils.getKeyId());
            staff.setCreateDate(DateUtils.getCurrent());
            staff.setDeleteMark(YesOrNoInteger.no);
            staff.setCreateUserId(getLoginModel().getLoginId());
            staff.setCreateUserName(getLoginModel().getLoginName());
            staff.setModelStatus(ModelStatus.add);
            Shop shop=(Shop)webSessionFactory.getCurrentLogin().getArgument("shop");
            staff.setShopId(shop.getShopId());
            staff.setCode(ChineseUtils.getPinYin(staff.getName()));
			staff.setMarkCode(ChineseUtils.getPinYinHeadChar(staff.getName()));
			if (StringUtils.isEmpty(staff.getSex())) {
				staff.setSex(SexStatus.unknown);
			} 
			
			authorityLoginUser.setModelStatus(ModelStatus.add);
        }else{
            staff.setModifyDate(DateUtils.getCurrent());
            staff.setModifyUserId(getLoginModel().getLoginId());
            staff.setModifyUserName(getLoginModel().getLoginName());
            staff.setModelStatus(ModelStatus.update);
            
            authorityLoginUser.setModelStatus(ModelStatus.update);
        }
        authorityLoginUser.setId(staff.getId());
		authorityLoginUser.setAccount(staff.getMobile());
		authorityLoginUser.setName(staff.getName());			
		authorityLoginUser.setPassword(newPassword);
		authorityLoginUser.setCreateDate(DateUtils.getCurrent());
		
        ActionResult actionResult;
        if(staffService.applyStaffAndRole(staff)&&loginUserService.applyLoginUser(authorityLoginUser)){
            actionResult=new ActionResult();
        }else{
            actionResult=new ErrorResult();
        }

        model.addAttribute("staff", staff);
        model.addAttribute(ManageContainer.RESULTKEY, actionResult);

        return view(detailedViewName);
    }

    @RequestMapping(value =deleteViewName, method = { RequestMethod.POST },produces="text/plain;charset=utf-8")
    public @ResponseBody String delete(String key){
        if(staffService.deleteFakeByKey(key)){
            return new JsonActionResult(ManageContainer.SUCCESS).toJson();
        }
        return new JsonActionResult(ManageContainer.ERROR).toJson();
    }
    
    @RequestMapping(value = checkMobileViewName, method = { RequestMethod.GET })
	public @ResponseBody String checkMobile(String mobile,String id){
		return String.valueOf(!staffService.checkMobile(mobile, id));
	}
}
